﻿using System;
using System.ComponentModel;
using System.Windows.Controls;
using LSA.Framework;

namespace Visuals
{
    public class TextSearchFilter
    {
        private string filterText = "";
        public TextSearchFilter(ICollectionView filteredView, TextBox textBox)
        {
            textBox.TextChanged += delegate
                                       {
                                           filterText = textBox.Text;
                                           filteredView.Filter = Filter;
                                       };
        }

        private bool Filter(object obj)
        {
            if (String.IsNullOrEmpty(filterText))
                return true;

            string str = ((WordInfo) obj).Word;
            if (String.IsNullOrEmpty(str))
                return false;

            int index = str.IndexOf(filterText, 0, StringComparison.InvariantCultureIgnoreCase);

            return index > -1;
        }
    }
}
